package scrum.gui.help;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import scrum.entity.Project;
import scrum.entity.ProjectsStore;
import scrum.entity.UserStory;

public class Logic {
	
	public boolean checkResponsible(Project project, String responsible){
		
		if(project.getDeveloper().equals(responsible))
			return true;
		if(project.getAnalyst().equals(responsible))
			return true;
		if(project.getInterfacer().equals(responsible))
			return true;
		if(project.getTester().equals(responsible))
			return true;
		if(responsible == null)
			return true;
		return false;
	}
	
	public boolean checkProgress(UserStory userStory){
		
		if(0<=userStory.getProgress() && userStory.getProgress()<=100){
			
			return true;
			
		}
		
		return false;
	}
	
	public boolean checkWeight(UserStory userStory){
		
		int bussiness = 0;
		int weight = 0;
		
		if(!userStory.getBusinessValue().equals("")){
			bussiness = Integer.parseInt(userStory.getBusinessValue());
		}
		
		if(!userStory.getWeight().equals("")){
			weight = Integer.parseInt(userStory.getWeight());
		}
		
		
		
		if(bussiness>=0 || bussiness <= 10){
			return true;
		}
		
		if(weight >= 0 || weight <= 10){
			return true;
		}
		
		
		return false;
	}
	
	public boolean checkProjectName(ProjectsStore projectStore, Project project, Project preProject){
		
		 int index = -1;
		 
		 int originalIndex = -1;
		 
		 for (int i = 0; i < projectStore.getProjectsList().toArray().length; i++) {
    		 
    		 if(projectStore.getProjectsList().get(i).getProjectName().equals(preProject.getProjectName())){
    			 originalIndex = i;
    			 break;
    		 }
 		}
		 
		 
    	 for (int i = 0; i < projectStore.getProjectsList().toArray().length; i++) {
    		 
    		 if(projectStore.getProjectsList().get(i).getProjectName().equals(project.getProjectName())){
    			 index = i;
    			 break;
    		 }
 		}
    	 
    	if(index == -1 || index == originalIndex){
    		return false;
    	}
		
		return true;
	}
	
	public boolean checkDateOrder(UserStory userStory){
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		Date startDate;
		Date dueDate;
		try {
			startDate = dateFormat.parse(userStory.getStartDate());
			dueDate = dateFormat.parse(userStory.getDueDate());
			if(dueDate.after(startDate)){
				return true;
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean checkDateOrder(Project project){
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		Date startDate;
		Date dueDate;
		try {
			startDate = dateFormat.parse(project.getStartDate());
			dueDate = dateFormat.parse(project.getDueDate());
			if(dueDate.after(startDate)){
				return true;
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public String checkUserStory(Project project, UserStory userStory){
		
		String message = null;
		
		if(!(project.getProjectName() == null)){
			if(!checkResponsible(project,userStory.getResponsible())){
				message = "Responsible should be one of the employees or left empty";
				return message;
			}
		}else{
			if(!userStory.getResponsible().equals("")){
				message = "You shouldn't assign a responsible, because project has not been saved";
				return message;
			}
		}
		
		if(userStory.getStory().equals("")){
			message = "User story should not be empty";
			return message;
		}
		
		if(!checkProgress(userStory)){
			
			message = "It should be a integer, and range from 0 to 100";
			return message;
		}
		
		if(!checkDateOrder(userStory)){
			message = "dueDate should be after startDate \n in the format DD-MM-YYYY";
			return message;
		}
		
		if(!checkWeight(userStory)){
			message = "Bussiness value and Weight should be in the range of 0 to 10";
			return message;
		}
		
		return message;
	}
	
	
	
	public String checkProject(ProjectsStore projectStore, Project project,Project preProject){
		
		String message = null;
		
		if(project.getProjectName().equals("")){
			message = "Project's name shouldn't \n be empty";
			return message;
		}
		

		if(checkProjectName(projectStore, project,preProject)){
			message = "Project's name already exists,choose another one";
		}
		
		if(project.getProjectOwner().equals("")){
			message = "An owner should be necessary";
			return message;
		}
		
		if(project.getScrumMaster().equals("")){
			message = "An Master should be necessary";
		}
		
		if(!checkDateOrder(project)){
			message = "dueDate should be after startDate \n in the format DD-MM-YYYY";
			return message;
		}
		
		
		return message;
	}

}
